﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ITS.Core.ViewModel.Contents;
using ITS.DAL.Contents;
using ITS.Core.Domain.Contents;
using ITS.Core.Domain.Common;
using ITS.DAL.Common;
using ITS.DAL.Concrete.EF.Common;
using ITS.Infrastructure.Constants;
using ITS.Infrastructure.Helpers;
using ITS.Website.Models;

namespace ITS.Website.Controllers
{
    public class ContentController : BaseController
    {
        private readonly IContentRepository _contentRepository;
        private readonly ICommonRepository _commonRepository;

        public ContentController(IContentRepository contentRepository, ICommonRepository commonRepository)
        {
            this._contentRepository = contentRepository;
            this._commonRepository = commonRepository;
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult ManageContent(int ID)
        {
            ManageContentViewModel model = PrepareManageContentViewModel(ID);

            return View(model);
        }

        [HttpPost, Authorize(Roles = "Administrator")]
        public ActionResult ManageContent(FormCollection collection)
        {
            ManageContentViewModel model = new ManageContentViewModel();
            if (TryUpdateModel(model))
            {
                model.SubCategories = BuildSubCategorySelectList(model.ContentCategoryID); 
               IList<ContentItem> contentItems = _contentRepository.Search(model.Keyword, model.SubCategoryID);
               foreach (var item in contentItems)
               {
                   model.ContentItems.Add(ContentItemModel.FromContentItem(item));
               }
            }
            return View(model);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult CreateContentItem(int ID)
        {
            CreateContentItemViewModel model = PrepareCreateContentItemViewModel(ID);
            return View(model);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult CreateNewsItem()
        {
            CreateContentItemViewModel model = PrepareCreateContentItemViewModel(ContentCategories.News);
            return View("CreateContentItem", model);
        }

        [HttpPost]
        public ActionResult CreateContentItem(FormCollection collection)
        {
            CreateContentItemViewModel model = new CreateContentItemViewModel();
            if (TryUpdateModel(model))
            {
                Int64 contentID = BizCodeGenerator.NextVal((int)BizIdType.ContentItemID);
                ContentItem contentItenm = new ContentItem()
                {
                    ContentID = contentID,
                    SubCategoryID = model.SubCategoryID,
                    CreatedDate = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    Active = true
                };
                _contentRepository.SaveContentItem(contentItenm);
                ContentItemDescription description = new ContentItemDescription()
                {
                    DescriptionID = Guid.NewGuid(),
                    ContentID = contentID,
                    LanguageID = model.LanguageID,
                    Title = model.Title,
                    Description = model.Description,
                    Content = model.Content,
                    SeName = SeoHelper.GetSeName(model.Title, true, true)
                };
                _contentRepository.SaveContentDescription(description);

                return RedirectToAction("EditContentItem", new { id = contentID });
            }
            return View(model);
        }

        public ActionResult EditContentItem(int ID)
        {
            EditContentItemViewModel model = PrepareEditContentItemViewModel(ID);
            return View(model);
        }

        [HttpPost]
        public PartialViewResult EditContentDescription(Guid descriptionID)
        {
            ContentItemDescription description = _contentRepository.ContentItemDescription(descriptionID);
            EditContentItemDescriptionViewModel model = new EditContentItemDescriptionViewModel()
            {
                DescriptionID = descriptionID,
                Title = description.Title,
                Description = description.Description,
                Content = description.Content
            };

            return PartialView("/Views/Content/PartialViews/EditContentItemDescription.cshtml", model);
        }
        [HttpPost]
        public ActionResult EditContentItemDescription_Post(FormCollection collection)
        {
            EditContentItemDescriptionViewModel model = new EditContentItemDescriptionViewModel();
            TryUpdateModel<EditContentItemDescriptionViewModel>(model);
            ContentItemDescription description = _contentRepository.ContentItemDescription(model.DescriptionID);
            description.Title = model.Title;
            description.Description = model.Description;
            description.Content = model.Content;
            description.SeName = SeoHelper.GetSeName(model.Title, true, true);
            _contentRepository.UpdateContentItemDescription(description);
            return RedirectToAction("EditContentItem", new { id = description.ContentID });
        }

        [HttpPost]
        public PartialViewResult AddContentDescription(Int64 contentID)
        {
            AddContentItemDescriptionViewModel model = new AddContentItemDescriptionViewModel();
            model.ContentID = contentID;
            model.SupportedLanguages = BuildSupportedLanguagesSelectList();

            return PartialView("/Views/Content/PartialViews/AddContentItemDescription.cshtml", model);
        }
        [HttpPost]
        public ActionResult AddContentItemDescription_Post(FormCollection collection)
        {
            AddContentItemDescriptionViewModel model = new AddContentItemDescriptionViewModel();
            TryUpdateModel<AddContentItemDescriptionViewModel>(model);
            ContentItemDescription description = new ContentItemDescription()
            {
                DescriptionID = Guid.NewGuid(),
                ContentID = model.ContentID,
                LanguageID = model.LanguageID,
                Title = model.Title,
                Description = model.Description,
                Content = model.Content,
                SeName = SeoHelper.GetSeName(model.Title, true, true)

            };
            _contentRepository.AddContentItemDescription(description);
            return RedirectToAction("EditContentItem", new { id = description.ContentID });
        }

        public ActionResult ContentItemDetail(int ID)
        {

            ContentItemDetailViewModel model = new ContentItemDetailViewModel();
            model.ContentItemDescription = _contentRepository.ContentItemDescription(ID, CultureHelper.CurrentCulture);
            return View(model);
        }

        #region Helpers
        private CreateContentItemViewModel PrepareCreateContentItemViewModel(int categoryID)
        {
            CreateContentItemViewModel model = new CreateContentItemViewModel();
            model.ContentCategory = _contentRepository.ContentCategory(categoryID);
            model.SubCategories = BuildSubCategorySelectList(categoryID);
            model.SupportedLanguages = BuildSupportedLanguagesSelectList();
            return model;
        }
        private ManageContentViewModel PrepareManageContentViewModel(int categoryID)
        {
            ManageContentViewModel model = new ManageContentViewModel();
            ContentCategory category = _contentRepository.ContentCategory(categoryID);
            model.ContentCategoryID = category.CategoryID;
            model.ContentCategoryName = category.Name;
            model.SubCategories = BuildSubCategorySelectList(categoryID);
            return model;
        }
        private IList<SelectListItem> BuildSubCategorySelectList(int categoryID)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            IQueryable<ContentSubCategory> subCategoryList = _contentRepository.SubCategoriesByCategory(categoryID);
            foreach (var sub in subCategoryList)
            {
                ContentSubCategoryDescription description = _contentRepository.DefaultSubCategoryDescription(sub.SubCategoryID);
                items.Add(new SelectListItem
                {
                    Text = description.Name,
                    Value = sub.SubCategoryID.ToString()
                });
            }

            return items;
        }
        private IList<SelectListItem> BuildSupportedLanguagesSelectList()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            IEnumerable<SupportedLanguage> list = _commonRepository.GetAllSupportedLanguages();
            foreach (var sub in list)
            {
                items.Add(new SelectListItem
                {
                    Text = sub.Name,
                    Value = sub.LanguageID
                });
            }

            return items;
        }
        private EditContentItemViewModel PrepareEditContentItemViewModel(int contentID)
        {
            EditContentItemViewModel model = new EditContentItemViewModel();
            ContentItem currentItem = _contentRepository.ContentItem(contentID);
            ContentCategory category = currentItem.ContentSubCategory.ContentCategory;

            model.ContentID = contentID;
            model.ContentCategory = category;
            model.SubCategories = BuildSubCategorySelectList(category.CategoryID);
            model.SupportedLanguages = BuildSupportedLanguagesSelectList();

            model.SubCategoryID = currentItem.SubCategoryID;
            model.Active = currentItem.Active;
            model.ContentItemDescriptions = currentItem.ContentItemDescriptions;
            return model;
        }
        #endregion
    }
}
